#!/bin/bash

#   Copyright (C) by Lieven De Samblanx ON7LDS
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#########################################################
#                                                       #
#           MMDVMHost configuration patcher             #
#                                                       #
#                   (c)2018 by ON7LDS                   #
#                                                       #
#                        V1.04                          #
#                                                       #
#  This program changes the MMDVMHost configuration     #
#   file to suit the needs of NextionDriver             #
#                                                       #
#########################################################

#Where do we find the data files (groups and DMR is's)
#for Pi-Star : /usr/local/etc/
DATAFILES="/usr/local/etc/"

#########################################################

if [ "$1" == "" ]; then
    echo "You have to specify the config file !"
    exit 1
fi

MMDVMConfig=$1
VERSION="1.04"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" #"


echo ""
echo "-----------------------------------------------------------"
echo "NOTE: I tried to make this script as foolproof as possible."
echo "  There might still be situations where it does not work"
echo "  as expected."
echo "  If you should discover any bugs, please let me know."
echo "-----------------------------------------------------------"
echo ""
echo "Starting NextionDriver_ConvertConfig $VERSION"
echo ""

if [[ -f "$MMDVMConfig" && -w "$MMDVMConfig" ]]; then
    echo "Processing ..."
else
    echo "The file is not a regular file or writable !"
    exit 2
fi

if [[ -f "$MMDVMConfig.old" ]]; then
    echo "There already is a backup file $MMDVMConfig.old"
    echo "This could be because NextionDriver already was installed earlier."
    echo "If this is the case, there probably is no need to run this convertor."
    echo "If, however, you are sure to run this convertor again,"
    echo " then please rename or remove $MMDVMConfig.old."
    echo ""
    echo "Mind you, running the convertor twice might result in a"
    echo " broken configuration"
    exit 3
fi

while IFS='' read -r line || [[ -n "$line" ]]; do
    if [ "$line" == "[NextionDriver]" ]; then
        echo "There already is a [NextionDriver] section."
        echo " I'm going to leave the file as is."
        echo " You'll have to change it by hand if necessary."
        exit 4
    fi
done < "$MMDVMConfig"

# Ok, alles lijkt goed.
mv "$MMDVMConfig" "$MMDVMConfig.old"


NEXTIONPORT_IS_MODEM=""
NEXTIONPORT_IS_DRIVER=""
TRANSPARENT=""
while IFS='' read -r line || [[ -n "$line" ]]; do
    #sectie bepalen
    if [[ "${line:0:1}" == "[" ]]; then
        SECTION=${line:1:-1}
    fi
    #Port van Nextion zoeken, 
    #we moeten weten of het modem is of niet
    if [[ "$SECTION" == "Nextion" && "$line" == "Port=modem" ]]; then
        NEXTIONPORT_IS_MODEM="1"
        echo "Info: Serial port for Nextion is modem."
        echo "      We will have to enable Transparent Data!"
    fi
    if [[ "$SECTION" == "Nextion" && "$line" == "Port="*"ttyNextionDriver" ]]; then
        NEXTIONPORT_IS_DRIVER="1"
        echo "ERROR: The driver already seems to be installed,"
        echo "       so I will not change the configuration."
        echo "       If something is wrong, please correct it by hand."
        mv "$MMDVMConfig.old" "$MMDVMConfig"
        exit 1
    fi
    if [[ "$SECTION" == "Transparent Data" && "$TRANSPARENT" == "" ]]; then
        TRANSPARENT="1"
        echo "Info: Transparent Data section found."
    fi
    if [[ "$SECTION" == "Transparent Data" && "$TRANSPARENT" == "1" && "$line" == "SendFrameType="* ]]; then
        TRANSPARENT="2"
        echo "Info: SendFrameType option found."
    fi
done < "$MMDVMConfig.old"



if [[ "$NEXTIONPORT_IS_MODEM" == "1" && "$TRANSPARENT" == "" ]]; then
    echo "Info: No Transparent Data section."
    echo "      Will be inserted !"
    TRANSPARENT="3"
fi


echo "--->  Creating new ini file..."
SECTION=""
SERIALPORT=""
FOUND_NEXTION=""
FOUND_MODEM=""
while IFS='' read -r line || [[ -n "$line" ]]; do
    #sectie bepalen
    if [[ "${line:0:1}" == "[" ]]; then
        SECTION=${line:1:-1}
    fi

    #[Modem]
    if [[ "$SECTION" == "Modem" ]]; then
        FOUND_MODEM="1"
    fi

    #[Transparent data]
    if [[ "$NEXTIONPORT_IS_MODEM" == "1" && "$SECTION" == "Transparent Data" ]]; then
        if [[ "${line:0:7}" == "Enable=" ]]; then
            TRANSPARENT_ENABLE=${line:7:1}
            echo "Info: Transparent data enable: $TRANSPARENT_ENABLE"
            if [[ "$TRANSPARENT_ENABLE" != "1" ]]; then
                echo "        changing to 1"
                line="Enable=1"
            else
                echo "        OK !"
            fi
            if [[ "$TRANSPARENT" == "1" ]]; then
                TRANSPARENT="x"
                echo "Info: Inserting SendFrameType=1"
                echo "$line" >> "$MMDVMConfig"
                line="SendFrameType=1"
            fi
        fi
        if [[ "$TRANSPARENT" == "2" && "${line:0:14}" == "SendFrameType=" ]]; then
            SENDFRAMETYPE=${line:14:1}
            echo "Info: SendFrameType: $SENDFRAMETYPE"
            if [[ "$SENDFRAMETYPE" != "1" ]]; then
                echo "        changing to 1"
                line="SendFrameType=1"
            else
                echo "        OK !"
            fi
        fi
    fi

    #[Nextion]
    if [[ "$SECTION" == "Nextion" ]]; then
        FOUND_NEXTION="1"
        #Port van Nextion gevonden ?
        # bewaren welke dat is !
        if [[ "${line:0:5}" == "Port=" ]]; then
            SERIALPORT="${line:5}"
            echo "Info: Serial port for Nextion was $SERIALPORT"
            echo "        changing to /dev/ttyNextionDriver"
            line="Port=/dev/ttyNextionDriver"
        fi

    fi

    #eerste sectie na [Nextion] gevonden ?
    # hier zetten we onze gegevens tussen
    if [[  "$SECTION" != "Nextion" && "$FOUND_NEXTION" == "1" && "${line:0:1}" == "[" ]]; then
        echo "Info: Inserting [NextionDriver] section"
        SECTION="[NextionDriver]\nPort=$SERIALPORT\nDataFilesPath=$DATAFILES\n"$(cat $DIR/NextionDriver.cfg)"\n"
        echo -e "$SECTION" >> "$MMDVMConfig"
        echo -e "\n\nInserted section :"
        echo "------------------------------"
        echo -e "$SECTION"
        echo "------------------------------"
        echo ""
        FOUND_NEXTION=""
        if [ "$SERIALPORT" == "" ]; then
            echo -e "WARNING : no serial port for Nextion found. Check your $MMDVMConfig\n"
        fi
    fi

    if [[ "$SECTION" != "Modem" && "$FOUND_MODEM" == "1" && "${line:0:1}" == "[" && "$TRANSPARENT" == "3" ]]; then
        echo "Info: Inserting [Transparent Data] section"
        SECTION="[Transparent Data]\nEnable=1\nRemoteAddress=127.0.0.1\nRemotePort=40094\nLocalPort=40095\nSendFrameType=1\n\n"
        echo -e "$SECTION" >> "$MMDVMConfig"
        echo -e "\n\nInserted section :"
        echo "------------------------------"
        echo -e "$SECTION"
        echo "------------------------------"
        echo ""
        FOUND_MODEM=""
    fi


    echo "$line" >> "$MMDVMConfig"

done < "$MMDVMConfig.old"

